package com.stuManager.service;

import com.alibaba.fastjson.JSONObject;
import com.stuManager.dao.StudentDao;
import com.stuManager.dao.UserDao;
import com.stuManager.factory.DaoFactory;
import com.stuManager.model.CommonResponseModel;
import com.stuManager.model.Student;
import com.stuManager.model.User;

import java.sql.SQLException;
import java.util.List;

/*
    StudentService
    处理学生信息相关的逻辑
    fetchStudentList(JSONObject json) 获取学生列表，可带分页、搜索
    requestNewStudent(Student student, String token) 新建学生信息
    requestEditStudent(Student student, String token) 编辑学生信息
    requestDeleteStudent(Student student, String token) 删除学生信息
 */
public class StudentService {
    private StudentDao dao;
    private UserDao userDao;
    public StudentService() {
        this.dao = DaoFactory.getStudentDao();
        this.userDao = DaoFactory.getUserDao();
    }

    public CommonResponseModel fetchStudentList(int page, int pageSize, String token) {
        return fetchStudentList(token, page, pageSize, "", "");
    }

    public CommonResponseModel fetchStudentList(String token, String searchColumn, String searchValue) {
        return fetchStudentList(token, 1, 10, searchColumn, searchValue);
    }

    public CommonResponseModel fetchStudentList(String token) {
        return fetchStudentList(1, 10, token);
    }

    public CommonResponseModel fetchStudentList(JSONObject json) {
        System.out.println("查询的json:\n" + json.toJSONString());
        int page = json.getIntValue("page");
        int pageSize = json.getIntValue("pageSize");
        String token = json.getString("token");
        String searchColumn = json.getString("searchColumn");
        String searchValue = json.getString("searchValue");
        if (null == searchColumn || null == searchValue) {
            return fetchStudentList(page, pageSize, token);
        }
        else  {
            return fetchStudentList(token, page, pageSize, searchColumn, searchValue);
        }
    }

    /*
        fetchStudentList
        获取学生列表
        page  页码
        pageSize 每页的数据条数
        token   用户token
        searchColumn  要搜索的字段名称：stuNumber学号 stuName名称 email邮箱 birthday生日 sex性别 notes备注
        searchValue  要搜索的值
     */
    public CommonResponseModel fetchStudentList(String token, int page, int pageSize, String searchColumn, String searchValue) {
        CommonResponseModel m = new CommonResponseModel();
        if (!verifyToken(token)) {
            m.setErrorCode(1);
            m.setMessage("未授权的用户");
            return m;
        }
        if (page<=1) page=1;
        if (pageSize<=1) pageSize=1;
        int total = this.dao.fetchStudentCount();
        // 计算最大页码
        int maxPage = (total + pageSize - 1) / pageSize;
        if (page > maxPage) page = maxPage;
        JSONObject json = new JSONObject();
        //获取学生列表和数据总数，以及当前页码一起返回
        List<Student> students = null;
        if (searchColumn.length()>0 && searchValue.length()>0) {
            students = this.dao.fetchStudentList(searchColumn, searchValue);
        }
        else {
            students = this.dao.fetchStudentList(page, pageSize);
        }
        json.put("total", total);
        json.put("data", students);
        json.put("page", page);
        json.put("pageSize", pageSize);
        m.setErrorCode(0);
        m.setMessage("查询成功");
        m.setResult(json);
        return m;
    }

     /*
        requestNewStudent
        创建一个新的学生
        token   用户token
     */
    public CommonResponseModel requestNewStudent(Student student, String token) {
        CommonResponseModel m = new CommonResponseModel();
        System.out.println("获取到新建学生请求student:\n" + student);
        if (!verifyToken(token)) {
            m.setErrorCode(1);
            m.setMessage("未授权的用户");
            return m;
        }
        if (student.getStuNumber().length()==0) {
            m.setErrorCode(4);
            m.setMessage("学号不能为空");
            return m;
        }
        if (student.getStuName().length()==0) {
            m.setErrorCode(5);
            m.setMessage("姓名不能为空");
            return m;
        }
        //查询是否已存在的学号
        List<Student> studentResult = this.dao.fetchStudentList("stuNumber", student.getStuNumber());
        if (studentResult.size()>0) {
            m.setErrorCode(2);
            m.setMessage("该学号已存在，请删除后再添加");
            return m;
        }
        //进行新增
        int result = this.dao.createNewStudent(student);
        if (result > 0) {
            m.setErrorCode(0);
            m.setMessage("新增学生" + student.getStuName() +"[" + student.getStuNumber() + "]成功!");
        }
        else {
            m.setErrorCode(3);
            m.setMessage("新增学生信息失败，请稍后再试");
        }
        return m;
    }

    public CommonResponseModel requestEditStudent(Student student, String token) {
        CommonResponseModel m = new CommonResponseModel();
        System.out.println("获取到编辑学生的请求student:\n" + student);
        if (!verifyToken(token)) {
            m.setErrorCode(1);
            m.setMessage("未授权的用户");
            return m;
        }
        if (student.getStuNumber().length()==0) {
            m.setErrorCode(4);
            m.setMessage("学号不能为空");
            return m;
        }
        List<Student> studentResult = this.dao.fetchStudentList("stuNumber", student.getStuNumber());
        if (studentResult.size()==0) {
            m.setErrorCode(2);
            m.setMessage("该学号不存在，无法编辑");
            return m;
        }
        //提交学生信息的相关更改
        int result = this.dao.updateStudent(student);
        if (result > 0) {
            m.setErrorCode(0);
            m.setMessage("编辑学生" + student.getStuName() +"[" + student.getStuNumber() + "]的信息成功!");
        }
        else {
            m.setErrorCode(3);
            m.setMessage("编辑学生信息失败，请稍后再试");
        }
        return m;
    }

    public CommonResponseModel requestDeleteStudent(Student student, String token) {
        CommonResponseModel m = new CommonResponseModel();
        System.out.println("获取到删除学生的请求student:\n" + student);
        if (!verifyToken(token)) {
            m.setErrorCode(1);
            m.setMessage("未授权的用户");
            return m;
        }
        if (student.getStuNumber().length()==0) {
            m.setErrorCode(4);
            m.setMessage("学号不能为空");
            return m;
        }
        List<Student> studentResult = this.dao.fetchStudentList("stuNumber", student.getStuNumber());
        if (studentResult.size()==0) {
            m.setErrorCode(2);
            m.setMessage("该学号不存在，无法删除");
            return m;
        }
        //提交学生信息的相关更改
        int result = this.dao.deleteStudent(student);
        if (result > 0) {
            m.setErrorCode(0);
            m.setMessage("删除学生" + student.getStuName() +"[" + student.getStuNumber() + "]的信息成功!");
        }
        else {
            m.setErrorCode(3);
            m.setMessage("删除学生信息失败，请稍后再试");
        }
        return m;
    }

    //验证token是否合规
    private boolean verifyToken(String token) {
        if (token == null || token.length() == 0) {
            return false;
        }
        try {
            User user = this.userDao.seekToken(token);
            return user != null;
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return false;
    }
}
